package com.mcintyret.site.utils;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * User: mcintyret2
 * Date: 05/03/2012
 */

public abstract class AnnotatedBeanMapProvider {

    @Resource
    private ConfigurableListableBeanFactory beanFactory;

    @PostConstruct
    private void getParentBeanFactory() {
        // Get the top-level bean factory
        // this can be removed when we remove channels - then we will only have one bean factory.
        while (beanFactory.getParentBeanFactory() != null) {
            beanFactory = (ConfigurableListableBeanFactory) beanFactory.getParentBeanFactory();
        }
    }

    protected Map<String, Object> getAnnotatedBeanMap(Class<? extends Annotation> annotation) {
        return beanFactory.getBeansWithAnnotation(annotation);
    }
}
